<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018\1\6 0006
 * Time: 14:58
 */
namespace app\admin\model;
use think\Model;

class AppException extends Model {
    // 自动写入时间戳
    protected $autoWriteTimestamp = true;

    protected $createTime = 'create_time';

    protected $updateTime = '';

    protected $dateFormat = 'Y-m-d H:i:s';
    /**
     * 获取app异常信息
     * @author 王柯 <wangke.email@foxmail.com>
     * @return mixed
     */
    public function getTypeAttr($value){
        $type = [
            0 => '',
            1 => 'crash',        //崩溃
            2 => '卡顿',         //卡顿
            3 => 'exception',   //异常信息
            4 => 'anr',         //应用程序无响应或者响应超时
        ];
        return $type[$value];
    }

    /**
     * 获取所有异常记录信息
     * @author 王柯 <wangke.email@foxmail.com>
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getAll($where = ''){
        $data = $this
            ->where($where)
            ->select();
        if($data){
            foreach ($data as $key => $val){
                $data[$key] = $val->toArray();
            }
        }
        return $data;
    }

    /**
     * 获取单条异常记录信息详情
     * @author 王柯 <wangke.email@foxmail.com>
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getOne($id){

        $data = $this
            ->where('id='.$id)
            ->find();
        if($data){
            $data = $data ->toArray();
        }

        return $data;
    }

    /**
     * 获取异常信息记录总数
     * @author 王柯 <wangke.email@foxmail.com>
     * @return int|string
     */
    public function getCount($where = ''){
        $count = $this
            ->where($where)
            ->count();
        return $count;
    }
}